package com.party.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.party.common.entity.MyPage;
import com.party.common.utils.PageUtil;
import com.party.entity.*;
import com.party.entity.PbBanner;
import com.party.entity.dto.QueryBannerDto;
import com.party.entity.dto.QueryDeviceDto;
import com.party.mapper.PbBannerMapper;
import com.party.service.PbBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.party.service.PbStationDeviceService;
import com.party.util.MoJiCommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * banner
 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-10-25
 */
@Service
public class PbBannerServiceImpl extends ServiceImpl<PbBannerMapper, PbBanner> implements PbBannerService {

    @Resource
    private PbStationDeviceService pbStationDeviceService;

    @Override
    public boolean add(PbBanner pbBanner) {
//        PbBanner PbBanner1=this.getOne(new LambdaQueryWrapper<PbBanner>()
//                .eq(PbBanner::getTitle,pbBanner.getTitle()).eq(PbBanner::getType,pbBanner.getType()));
//        if(PbBanner1 !=null){
//            throw new RuntimeException("标题已存在,请核查!");
//        }
//        if(ObjectUtil.isEmpty(pbBanner.getSort())){
//            pbBanner.setSort(1);
//        }
        pbBanner.setOperatorId(MoJiCommonUtils.getUserCode());
        pbBanner.setStatus(1);
        pbBanner.setCreateTime(new Date());
        pbBanner.setUpdateTime(new Date());
        this.save(pbBanner);
        Long maxId=pbBanner.getId();
        if(ObjectUtil.isNotEmpty(pbBanner.getDeviceCode())){
            List<String> ids= Arrays.asList(pbBanner.getDeviceCode().split(","));
            for(String id:ids){
                PbStationDevice pbStationDevice=new PbStationDevice();
                pbStationDevice.setStationId(maxId);
                pbStationDevice.setDeviceId(Long.valueOf(id));
                pbStationDevice.setCreateTime(new Date());
                pbStationDevice.setType(pbBanner.getType());
                pbStationDeviceService.save(pbStationDevice);
            }
        }
      if(pbBanner.getType()==2)
      {
          List<PbBanner> list = this.list(new LambdaQueryWrapper<PbBanner>()
                  .eq(PbBanner::getType, 2).eq(PbBanner::getOperatorId,MoJiCommonUtils.getUserCode())
          );
          if (list != null) {
              for (int i = 0; i < list.size(); i++) {
                  PbBanner pbBanner1 = list.get(i);
                  if(!pbBanner1.getId().equals(maxId))
                  {
                      pbBanner1.setType(4);
                      this.updateById(pbBanner1);
                  }
              }
          }

          
      }
        return true;
    }

    @Override
    public boolean edit(PbBanner pbBanner) {
        PbBanner PbBanner1=this.getById(pbBanner.getId());
//        if(!pbBanner.getTitle().equals(PbBanner1.getTitle())){
//            PbBanner PbBanner2=this.getOne(new LambdaQueryWrapper<PbBanner>()
//                    .eq(PbBanner::getTitle,pbBanner.getTitle()).eq(PbBanner::getType,pbBanner.getType()));
//            if(PbBanner2 !=null){
//                throw new RuntimeException("标题已存在,请核查!");
//            }
//        }
        BeanUtils.copyProperties(pbBanner,PbBanner1);
        PbBanner1.setOperatorId(MoJiCommonUtils.getUserCode());
        PbBanner1.setUpdateTime(new Date());
        return this.updateById(PbBanner1);
    }

    @Override
    public boolean delete(Long id) {
        return this.removeById(id);
    }

    @Override
    public MyPage<PbBanner> queryPage(QueryBannerDto dto) {
        LambdaQueryWrapper<PbBanner> wrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(dto.getTitle())) {
            wrapper.like(PbBanner::getTitle, dto.getTitle());
        }
        if (ObjectUtil.isNotEmpty(dto.getType())) {
            wrapper.like(PbBanner::getType, dto.getType());
        }
        //管理员查看全部,非管理员查看各自的
        String loginId=MoJiCommonUtils.getLoginId();
        if(!"admin".equals(loginId)){
            wrapper.like(PbBanner::getOperatorId, MoJiCommonUtils.getUserCode());
        }
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        if (ObjectUtil.isNotEmpty(dto.getType()) && dto.getType()==2) {
            wrapper.orderByDesc(PbBanner::getNian,PbBanner::getMonth);
        }else{
            wrapper.orderByAsc(PbBanner::getSort);
        }
        List<PbBanner> deviceList = this.list(wrapper);
        PageInfo<PbBanner> info = new PageInfo<>(deviceList);
        return PageUtil.PageInfo2MyPage(info);
    }

    @Override
    public PbBanner detail(Long id) {
        return this.getById(id);
    }
}
